function [t,T,nomForces] = loadThrustData(filename,plotThings)
if nargin == 1
    plotThings = 0;
end



% filename = 'staticTestData/demoStaticTest.txt';
% plotThings = 1;


%% get data

data = load(filename);

tprelim = data(:,1);
Fprelim = data(:,2)*4.448;


if plotThings
    figure
    plot(tprelim,Fprelim)
    title('raw')
end

%% find slope of data
slope = (Fprelim(2:end) - Fprelim(1:end-1))./(tprelim(2:end) - tprelim(1:end-1));

%% choose a reference 'max' point using either maximum slope or maximum
%% thrust force
% [dum,mSlopeInd] = max(slope);
[~,mSlopeInd] = max(Fprelim);

%% find a start index of useful data by finding the first point with a
%% negative slop prior to the 'mSlopeInd' found above, so find the turning
%% point before the first peak, use as start of data
startInd = find(slope(1:(mSlopeInd-10)) < 0,1, 'last');

% if method of finding starting point didn't work, assume the data has
% already been worked on and we're at the beginning, use the beginning
if isempty(startInd)
    startInd = 1;
end


%% find an index corresponding to somewhere just after (0.3 sec after as
%% written) the firing
dumInd = find(tprelim > (tprelim(startInd) + 0.3),1,'first');

% if this didn't work, assume the data is too short and has already been
% fixed, no 'zero force' values can be calculated, if it did work, use the
% end of the data set past dumInd to calculate a nominal force that is used
% to rezero the data set
if isempty(dumInd)
    nomForce = 0;
    dumInd = length(tprelim);
else
    nomForce = mean(Fprelim(dumInd:end));
end


%% find an index corresponding to somewhere just before the firing to calculate 
%% nominal force before firing
dumInd2 = find(tprelim < (tprelim(startInd) - .2),1,'first');

%if it didn't work, the data is probably too short, so set the val to zero
if isempty(dumInd2)
    nomForceInit = 0;
    dumInd2 = 1;
else
    nomForceInit = mean(Fprelim(1:dumInd2));
end

% if desired, plot the nominal zeroing force along with the raw data
% plotted above
if plotThings
    hold all
    xx = [tprelim(1),tprelim(end)]; yy = [nomForce,nomForce];
    yy2 = [nomForceInit, nomForceInit];
% figure
    plot(xx,yy,'g',xx,yy2,'r',tprelim(startInd),Fprelim(startInd),'mx')
    legend('Data','Nom After Firing','Nominal Before Firing')
end

% subtract off the baseline-zero force
% F2 = Fprelim - nomForce;
F2 = Fprelim; %don't subtract off the baseline zero force

% cut data set down to index's determined above
t2s = tprelim(startInd:dumInd);
F2s = F2(startInd:dumInd);

% make another set of beginning/end indices corresponding to where the data
% dips below zero 
% --FIXME - I think this might not make much sense.... - pete
newStart = find(F2s > 0,1,'first');
newEnd = find(F2s > 0,1,'last');

% cut data set down to indexes just determined
t3s = t2s(newStart:newEnd); F3s = F2s(newStart:newEnd);

% shift time to start at zero
t = t3s - t3s(1);
T = F3s;

% add two data points to the end of the loaded/fixed set that correspond to
% 0 thrust, one at just past the last time step and one at 1000 seconds so 
% that for up to 1000 seconds, any time can be interpolated
t = [t;t(end)+1e-10;1000];
T = [T;0;0];

% plot final results if desired
if plotThings
    figure
    plot(t,T)
    grid on
    axis([0,1,0,300])
end

% multiply any negative values by zero...
Tmult = T > 0;
T = T.*Tmult;
nomForces = [nomForceInit,nomForce];
